const product = require('../../templates/product/list');

Page({
  data: {
    width: 200,   // header宽度
    height: 64,   // header高度
    inputTop: 0,  // 搜索框距离顶部距离
    loading: true,
    layoutShow: false,
    hotSearch: [],
    notify: [],
    banner: [],
    category: [],
    newProduct: [],
    hotProduct: [],
    recProduct: [],
    guessYouLike: []
  },
  onLoad: function() {
    let obj = wx.getMenuButtonBoundingClientRect();
    this.setData({
      width: obj.left - 60,
      height: obj.top + obj.height + 8,
      inputTop: obj.top / 2 + 5,
      isList: wx.getStorageSync('rf_layout')['index']
    });
    this.getIndexData();
  },
  onPageScroll: function (e) {
    let query = wx.createSelectorQuery(),
        show = this.data.layoutShow;
    query.select('#abc').boundingClientRect(rect => {
      if ((rect.top <= 180 && !show) || (rect.top > 180 && show)) {
        this.setData({
          layoutShow: !show
        });
      }
    }).exec();
    this.setData({
      scrollTop: e.scrollTop
    });
  },
  onPullDownRefresh: function () {
    this.getIndexData();
    wx.stopPullDownRefresh();
  },
  onShareAppMessage: function () {
    let conf = wx.getStorageSync('rf_config');
    return {
      title: conf.share_title,
      imageUrl: conf.share_cover
    }
  },
  getIndexData: async function () {
    wx.showLoading({
      title: "加载中",
      "mask": true
    });
    wx.util.request('common.index').then(res => {
      this.setData({
        loading: false,
        hotSearch: res.search.hot_search_list,
        banner: res.adv.index_top,
        category: {
          param: 'cate_id',
          list: res.cate
        },
        newProduct: {
          param: 'is_new',
          value: '1',
          list: res.product_new
        },
        hotProduct: {
          param: 'is_hot',
          value: '1',
          list: res.product_hot
        },
        recProduct: {
          param: 'is_recommend',
          value: '1',
          list: res.product_recommend
        },
        guessYouLike: res.guess_you_like
      });
    }).catch(err => {
      console.log(err);
    });
    wx.util.request('common.notify.list', '', '', 1).then(res => {
      this.setData({
        notify: res
      });
    });
    wx.util.request('product.brand', '', '', 1).then(res => {
      this.setData({
        brand: {
          param: 'brand_id',
          list: this.filterData(res, 3)
        }
      });
    });
    // 暂不启用，待其它基础功能和细节处理完毕后，完善营销再开启
    //this.getMarketing();
    /*let indexData = {};
    // 首页默认数据
    let common = await wx.util.request('common.index');
    if (common) {
      indexData.hotSearch = common.search.hot_search_list;
      indexData.banner = common.adv.index_top;
      indexData.category = {
        param: 'cate_id',
        list: common.cate
      };
      indexData.newProduct = {
        param: 'is_new',
        value: '1',
        list: common.product_new
      };
      indexData.hotProduct = {
        param: 'is_hot',
        value: '1',
        list: common.product_hot
      };
      indexData.recProduct = {
        param: 'is_recommend',
        value: '1',
        list: common.product_recommend
      };
      indexData.guessYouLike = common.guess_you_like;
    }
    // 公告
    let notify = await wx.util.request('common.notify.list');
    if (notify) {
      indexData.notify = notify;
    }
    // 品牌
    let brand = await wx.util.request('product.brand');
    if (brand) {
      brand = this.filterData(brand, 3);
      indexData.brand = {
        param: 'brand_id',
        list: brand
      }
    }
    // 限时折扣
    let {
      hasMarketing,
      marketing
    } = await this.getMarketing();
    indexData.hasMarketing = hasMarketing;
    indexData.marketing = marketing;
    this.setData(indexData);*/
  },
  getMarketing: async function () {
    let prefix = 'marketing.', hasMarketing = false;
    var discount = await wx.util.request(prefix + 'discount', '', '', 1);
    var bargain = await wx.util.request(prefix + 'bargain', '', '', 1);
    var wholesale = await wx.util.request(prefix + 'wholesale.list', '', '', 1);
    var group = await wx.util.request(prefix + 'groupBuy', '', '', 1);
    if (discount.length > 0 || bargain.length > 0 || wholesale.length > 0 || group.length > 0) {
      hasMarketing = true;
    }
    this.setData({
      hasMarketing: hasMarketing,
      marketing: {
        discount: {
          icon: '/static/images/marketing/icon-discount-label.png',
          title: '限时折扣',
          list: this.filterData(discount)
        },
        bargain: {
          icon: '/static/images/marketing/icon-bargain.png',
          title: '疯狂砍价',
          list: this.filterData(bargain)
        },
        wholesale: {
          icon: '/static/images/marketing/icon-wholesale.png',
          title: '我要拼团',
          list: this.filterData(wholesale)
        },
        groupBuy: {
          icon: '/static/images/marketing/icon-group.png',
          title: '团购好物',
          list: this.filterData(group)
        }
      }
    });
    /*return {
      hasMarketing: hasMarketing,
      marketing: {
        discount: {
          icon: '/static/images/marketing/icon-discount-label.png',
          title: '限时折扣',
          list: this.filterData(discount)
        },
        bargain: {
          icon: '/static/images/marketing/icon-bargain.png',
          title: '疯狂砍价',
          list: this.filterData(bargain)
        },
        wholesale: {
          icon: '/static/images/marketing/icon-wholesale.png',
          title: '我要拼团',
          list: this.filterData(wholesale)
        },
        groupBuy: {
          icon: '/static/images/marketing/icon-group.png',
          title: '团购好物',
          list: this.filterData(group)
        }
      }
    };*/
  },
  filterData: function (list, num = 2) {
    var res = [];
    if (list && list.length > 0) {
      let arr = [];
      for (let i = 0; i < num; i++) {
        if (!list[i]) break;
        arr.push(list[i]);
      }
      res = arr;
    }
    return res;
  },
  layout: function () {
    let layout = wx.getStorageSync('rf_layout'),
        isList = !this.data.isList;
    layout.index = isList;
    wx.setStorageSync('rf_layout', layout);
    this.setData({
      isList: isList
    });
  },
  news: function (e) {
    let id = e.currentTarget.dataset.id;
    let page = (id == 'more' ? 'index' : 'detail');
    wx.navigateTo({
      url: `../news/${page}?id=${id}`,
    })
  },
  search: function () {
    wx.navigateTo({
      url: '../search/index'
    })
  },
  detail: function (e) {
    product.detail(e);
  },
  coupon: function () {
    wx.navigateTo({
      url: '../coupon/index'
    })
  },
  classify: function () {
    wx.switchTab({
      url: '../category/index'
    });
  },
  more: function (e) {
    product.more(e);
  },
  marketing: function (e) {
    let page = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: `../marketing/${page}`,
    })
  },
  similar: function (e) {
    product.similar(e.currentTarget.dataset.kw);
  }
})